//
//  MZView_Labels.swift
//  MZTool
//
//  Created by gby on 2023/4/25.
//

import UIKit

open class MZLabesl_Config{
    public var textfont: CGFloat = 12
    public var textcolor: UIColor
    public var labBgColor: UIColor
    public var labBorderColor: UIColor
    public var labWidth: CGFloat = 0
    public var space: CGFloat = 0
    public var inset: UIEdgeInsets = UIEdgeInsets.init(top: 2, left: 10, bottom: 2, right: 10)
    
    init(font: CGFloat, color: UIColor, labBgColor: UIColor, labBorderColor: UIColor, labWidth: CGFloat, space: CGFloat) {
        self.textfont = font
        self.textcolor = color
        self.labBgColor = labBgColor
        self.labBorderColor = labBorderColor
        self.labWidth = labWidth
        self.space = space
    }
}


open class MZView_Labels: UIView {

    private var currentConfig: MZLabesl_Config = ({
        let c = MZLabesl_Config.init(font: 12, color: .black, labBgColor: MZ.RGB("#F4F4F4"), labBorderColor: MZ.RGB("#F4F4F4"), labWidth: 0, space: 5)
        return c
    })()
    public var labels: [String] = [] {
        didSet{
            reload_UI()
        }
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    public func update_Config( _ updatec:(_ config: MZLabesl_Config) -> Void) -> MZView_Labels {
        updatec(self.currentConfig)
        return self
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload_UI() {
        while self.subviews.count > 0 {
            self.subviews.last?.removeFromSuperview()
        }
        
        var top: CGFloat = 0
        var left: CGFloat = 0
        let maxW = self.frame.width
        var lastView: UIView?
        for labstr in labels {
            let labbview: UIView = UIView.init()
            labbview.backgroundColor = currentConfig.labBgColor
            labbview.layer.borderColor = currentConfig.labBorderColor.cgColor
            labbview.layer.borderWidth = currentConfig.labWidth
            let labb: UILabel = UILabel.init(currentConfig.textfont, color:currentConfig.textcolor)
            labbview.addSubview(labb)
            labb.text = labstr
            labb.sizeToFit()
            labb.mz.top = currentConfig.inset.top
            labb.mz.left = currentConfig.inset.left
            
            labbview.frame = CGRect.init(x: left,
                                         y: top,
                                         width: labb.mz.width + (currentConfig.inset.left + currentConfig.inset.right),
                                         height: labb.mz.height + (currentConfig.inset.top + currentConfig.inset.bottom))
            
            labbview.layer.cornerRadius = labbview.mz.height/2
            //超过最大
            if labbview.mz.right + currentConfig.space >= maxW {
                left = 0
                top += (lastView?.mz.height ?? 0) + currentConfig.space
                labbview.mz.left = left
                labbview.mz.top = top
            }
            left = labbview.mz.right + currentConfig.space
            self.addSubview(labbview)
            
            lastView = labbview
        }
        top += lastView?.mz.height ?? 0
        
        self.mz.height = top
    }
    
}
